(function() {
  // vconsole
  window.uniConsole = function() {
    if (!ab_.vconsoled) {
      ab_.vconsoled = true
      ab_.reqJs('https://cdn.bootcss.com/vConsole/3.3.4/vconsole.min.js', 0, function() {
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.appendChild(document.createTextNode('new VConsole()'));
        document.body.appendChild(script);
      })
    }
  }

  if (ab_.vconsoled) {
    // 预设置
    ab_.vconsoled = false
    uniConsole()
  }

  var uniStart = ab_.newStart(function() {
    var get = 0
    try {
      window.$evn = {}
      uni.getEnv(function(res) {
        window.$evn = res
        uniReady.start()
      })

      get = 1
      // uni.getEnv 有可能不回调
      setTimeout(uniReady.start, 3000)

    } finally {
      if (!get) {
        uniReady.start()
      }
    }
  })

  uniStart.waite()
  if (window.uni && window.uni.getSystemInfo) {
    // uni项目
    uniStart()

  } else {
    var reqs = []
    var agent = navigator.userAgent
    ab_.weixin = agent.match(/MicroMessenger/i) == "micromessenger"
    if (ab_.weixin) {
      // 微信小程序
      reqs.push('https://res.wx.qq.com/open/js/jweixin-1.4.0.js')
    }

    uniStart.waite()
    document.addEventListener('UniAppJSBridgeReady', function() {
      uniStart()
    })

    ab_.reqJs('https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js', 0, function() {
      var i = 0

      function reqNext() {
        if (i < reqs.length) {
          ab_.reqJs(reqs[i], 0, reqNext)

        } else {
          uniStart()
          if (window.uni) {
            setTimeout(uniStart, 100)
          }
        }
      }

      reqNext()
    })
  }

  // 代理方法
  var actI = 0
  var actBacks = {}

  function actBackReg(success, fail, complete) {
    actI = actI >= 64 ? 0 : (actI + 1)
    actBacks[actI] = [success, fail, complete]
    return actI
  }

  function actBackRegP(paras) {
    var actI = actBackReg(paras.success, paras.fail, paras.complete)
    //delete paras.success
    //delete paras.fail
    //delete paras.complete
    return actI
  }

  function actBackCall(i, t, paras) {
    var backs = actBacks[parseInt(i)]
    if (backs) {
      try {
        var back = backs[parseInt(t)]
        if (back) {
          back.apply(null, paras || [])
        }

      } catch (e) {
        console.error(e)

      } finally {
        if (t === 3) {
          delete actBacks[i]
        }
      }

    } else {
      console.error('actBackCall err at = ' + i)
    }
  }

  if (ab_.h5) {
    ab_.historyL = history.length
  }

  var $center = ab_.$center = {
    // 调试模式
    vConsole: function() {
      uniConsole()
    },
    // 方法回调
    actBack: function(paras) {
      try {
        paras = JSON.parse(paras)
        actBackCall(paras[0], paras[1], paras[2])

      } catch (e) {
        console.error(e)
      }
    },
    // h5清理浏览
    backRoot: function() {
      if (history.length > ab_.historyL) {
        history.back(history.length - ab_.historyL)
      }
    },
  }

  try {
    //plug bug fixed
    if (ab_.uniApp) {
      var postEvent = ab_.postEvent
      ab_.postEvent = function(event, paras) {
        if (plus.webview.currentWebview().parent()) {
          postEvent(event, paras)
        }
      }
    }

  } catch (e) {
    console.log(e)
  }

  // 接收消息
  ab_.eid = ''
  window.onmessage = ab_.onmessage = function(e) {
    var name = typeof(e)
    if (name === 'object') {
      onmessage(e.data)

    } else if (name === 'string') {
      //console.log(e)
      name = e
      var paras = e.indexOf(',')
      if (paras > 0) {
        name = e.substring(0, paras)
        paras = e.substring(paras + 1)

      } else {
        paras = undefined
      }

      //console.log(name + ', ' + paras)
      if (name === 'report') {
        ab_.eid = paras ? (paras[0] || '') : ''
        window.$postMessage('report', ab_.eid + ',' + location.href)
        return
      }

      e = $center[name]
      if (e) {
        e.apply(null, [paras])
        return
      }

      e = window.$onMessage ? window.$onMessage[name] : undefined
      if (e) {
        e.apply(null, [paras])
        return
      }

      console.error('onmessage err at ' + name + '|' + name.length + '|' + paras)
    }
  }

  // 发送消息
  if (!window.$postMessage) {
    window.$postMessage = function(name, paras) {
      if (paras) {
        name = name + ',' + paras
      }

      uni.navigateTo({
        url: '/pages/transfer?t=' + encodeURIComponent(name),
      })
    }
  }

  // uni 代理方法
  window.$uniAct = ab_.$uniAct = function(act, paras) {
    var actI = actBackRegP(paras)
    paras._act = act
    paras._actI = actI
    $postMessage('uniAct', JSON.stringify(paras))
  }

  // ab 代理方法
  window.$abAct = ab_.$abAct = function(act, paras, complete) {
    var actI = actBackReg(null, null, complete)
    $postMessage('abAct', JSON.stringify([act, paras, actI]))
  }

  // token 通知
  uniReady(function() {
    $abAct('token', 0, function(err, token) {
      console.log("token = " + token)
      if (token) {
        window.onmessage('login,' + token)
      }
    })

    // window.onhashchange = function() {
    // 	window.$postMessage('report', ab_.eid + ',' + location.href)
    // }
    window.addEventListener("hashchange", function() {
      window.$postMessage('report', ab_.eid + ',' + location.href)
    })
    var pushState = history.pushState
    var replaceState = history.replaceState
    history.pushState = function() {
      pushState.apply(this, arguments)
      window.$postMessage('report', ab_.eid + ',' + location.href)
    }
    history.replaceState = function() {
      replaceState.apply(this, arguments)
      window.$postMessage('report', ab_.eid + ',' + location.href)
    }
  })

  uniStart()

})()
